<?php
/**
 * Return text description for credential key provides in argument
 * @param string $right the constant right available in LetsManagerRights
 */
function displayNameRight($right)
{
  return LetsManagerRights::getDisplayNameByCode($right);
}
/**
 * Return text name for money used in association of current user
 * @param integer $asso_id
 */
function lets_money_name($asso_id = -1)
{
  if($asso_id == -1)
  {
    $asso_id = sfContext::getInstance()->getUser()->getAssociationId();
  }
  return Configurator::get(LetsConfiguration::MONEY_NAME,$asso_id);
}
/**
 * Return available money for user id in argument or current user if no id specified
 * @param integer $user_id
 * @return string html text to display amount money and money name
 */
function lets_member_money($user_id = -1,$displayMoneyName = true)
{
  if($user_id == -1)
  {
    $user_id = sfContext::getInstance()->getUser()->getUserId();
  }
  $money = LetsMoneyAccountTable::getMoneyAvailable($user_id);
  $text =''.$money;
  if($displayMoneyName)
  {
    $myAssoId = sfContext::getInstance()->getUser()->getAssociationId();
    $text .= ' '.lets_money_name($myAssoId);
  }

  if($money < 0)
  {
    $text = '<span style="color:red;">'.$text.'</span>';
  }

  return $text;
}
/**
 * Display sentence with current money of user in argument,
 * if the amount is < 0 we add red color on the amount
 * @param $user_id the user id
 * @return string html to display the sentence with amount
 */
function lets_member_money_sentence($user_id =-1)
{
  $myUserId = sfContext::getInstance()->getUser()->getUserId();

  $sentence = 'Vous disposez à ce jour de';
  if($myUserId != $user_id && $user_id != -1)
  {
    $member = MemberTable::getById($user_id);
    $sentence = ''.$member.' dispose à ce jour de';
  }
  return $sentence.' '.lets_member_money($user_id,true).'.';
}
/**
 * Format member with link
 * @param Member or sfGuardUser $member
 * @param boolean $popup open link on new window if true
 * @param boolean $show_username add username between () after name
 * @return string html generated with link to member details
 */
function lets_format_member($member, $popup = true,$show_username = true)
{
  if ($member instanceof sfOutputEscaper)
  {
    $member = $member->getRawValue();
  }
  $str ='';
  $memberIdAssociation = 1;
  if($member instanceof Member)
  {
    $memberIdAssociation = LetsConfiguration::memberIdForAssociation($member->getAssociationId());
  }
  $memberName = $member->getName();
  $memberIsEnabled = false;
  if($member instanceof sfGuardUser)
  {
    $memberIsEnabled = $member->getIsActive();
  }
  // member type
  else
  {
    $memberIsEnabled = ($member->getState() == MemberTable::STATE_ENABLED);
  }
  if (!$member->exists())
  {
    $str = '<i>Système</i>';
  }
  else if($member->getId() == $memberIdAssociation)
  {
    $str =  $member->getName();
  }
  else
  {
    if ($memberIsEnabled)
    {
      $link = '@lets_user_show?id=' . $member->getId();
      if($show_username)
      {
        $memberName .= ' (' .$member->getUsername(). ')';
      }
      $str = link_to($memberName,$link,array('popup'=>$popup));
    }
    else
    {
      $str = '<del>'.$memberName.'</del>';
    }
  }

  return $str;
}


?>